package com.rgp.serviceprice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rgp.internalcommon.common.CommonStatusEnum;
import com.rgp.internalcommon.dto.FareTypeDto;
import com.rgp.internalcommon.dto.ResponseResult;
import com.rgp.internalcommon.dto.TollRuleDto;
import com.rgp.internalcommon.response.FareVersionResponse;
import com.rgp.serviceprice.mapper.TollRuleMapper;
import com.rgp.serviceprice.pojo.TollRule;
import com.rgp.serviceprice.service.TollRuleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author nhp
 * @version 1.0
 * @description TODO
 * @date 2024-04-21 11:51
 */
@Service
public class TollRuleServiceImpl implements TollRuleService {

    @Autowired
    private TollRuleMapper tollRuleMapper;

    @Override
    public ResponseResult addTollRule(TollRuleDto tollRuleDto) {

        //1.获取当前计价规则的城市编号 与 载具类型
        String cityCode = tollRuleDto.getCityCode();
        Integer vehicleType = tollRuleDto.getVehicleType();
        String fareType = cityCode + "$" + vehicleType;
        //2.根据fareType字段进行计价规则的查询
        Map<String, Object> map = new HashMap<>();
        map.put("fare_type",fareType);
        List<TollRule> tollRules = tollRuleMapper.selectByMap(map);
        //如果当前存在[cityCode + "$" + vehicleType]的计价规则，则不允许添加
        if (tollRules != null && tollRules.size() > 0){
            return ResponseResult.fail(CommonStatusEnum.TOLL_RULE_NOT_EXIST);
        }
        TollRule tollRule = new TollRule();
        BeanUtils.copyProperties(tollRuleDto,tollRule);
        tollRule.setFareType(fareType);
        tollRule.setFareVersion(1);
        int insert = tollRuleMapper.insert(tollRule);
        if (insert != 1){
            return ResponseResult.fail(CommonStatusEnum.CREATE_TOLL_RULE_ERRIR);
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult editTollRule(TollRuleDto tollRuleDto) {

        //1.获取当前计价规则的城市编号 与 载具类型
        String cityCode = tollRuleDto.getCityCode();
        Integer vehicleType = tollRuleDto.getVehicleType();
        String fareType = cityCode + "$" + vehicleType;
        //2.根据fareType字段进行计价规则的查询
        QueryWrapper<TollRule> wrapper = new QueryWrapper<>();
        wrapper.eq("fare_type",fareType);
        wrapper.orderByDesc("fare_version");
        List<TollRule> tollRules = tollRuleMapper.selectList(wrapper);
        //计价规则不存在，无法修改
        if (tollRules == null || tollRules.size() == 0){
            return ResponseResult.fail(CommonStatusEnum.TOLL_RULE_NOT_EXIST);
        }
        //计价规则存在，比较当前修改后的计价规则 与 修改前的计价规则之间的差异性，若不存在差异性，则无法修改
        TollRule existTollRule = tollRules.get(0);
        Double startFare = existTollRule.getStartFare();
        Double startMile = existTollRule.getStartMile();
        Double unitPricePerMile = existTollRule.getUnitPricePerMile();
        Double unitPricePerMinute = existTollRule.getUnitPricePerMinute();
        if (startFare.equals(tollRuleDto.getStartFare())
                && startMile.equals(tollRuleDto.getStartMile())
                && unitPricePerMile.equals(tollRuleDto.getUnitPricePerMile())
                && unitPricePerMinute.equals(tollRuleDto.getUnitPricePerMinute())
        ){
            return ResponseResult.fail(CommonStatusEnum.UPDATE_EXIST_TOLL_RULE_ERRIR);
        }
        //3.更新计价规则
        int existFareVersion = Integer.parseInt(existTollRule.getFareVersion().toString());
        TollRule tollRule = new TollRule();
        BeanUtils.copyProperties(tollRuleDto,tollRule);
        tollRule.setFareType(fareType);
        tollRule.setFareVersion(++existFareVersion);
        //以插入一条计价规则数据的方式的进行更新
        int insert = tollRuleMapper.insert(tollRule);
        if (insert != 1){
            return ResponseResult.fail(CommonStatusEnum.UPDATE_TOLL_RULE_ERRIR);
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult<FareVersionResponse> getNewestFareVersion(FareTypeDto fareTypeDto) {
        QueryWrapper<TollRule> wrapper = new QueryWrapper<>();
        wrapper.eq("fare_type",fareTypeDto.getFareType());
        wrapper.orderByDesc("fare_version");
        List<TollRule> tollRules = tollRuleMapper.selectList(wrapper);
        TollRule tollRule = tollRules.get(0);
        FareVersionResponse response = new FareVersionResponse();
        response.setFareVersion(tollRule.getFareVersion());
        return ResponseResult.success(response);
    }

    @Override
    public ResponseResult<Boolean> ifExists(FareTypeDto fareTypeDto) {
        QueryWrapper<TollRule> wrapper = new QueryWrapper<>();
        wrapper.eq("fare_type",fareTypeDto.getFareType());
        List<TollRule> tollRules = tollRuleMapper.selectList(wrapper);
        if (tollRules != null && tollRules.size() > 0){
            return ResponseResult.success(true);
        }
        return ResponseResult.success(false);
    }
}